import {
  Controller,
  Get,
  Post,
  Headers,
  Body,
  Param,
  Delete,
  Query,
  Res,
  Put,
  ClassSerializerInterceptor,
  UseInterceptors,
  Patch,
  UploadedFile,
} from "@nestjs/common";
import { Response } from "express";
import { FileInterceptor } from "@nestjs/platform-express";
import { UserService } from "./user.service";
import { UserDto } from "./dto/user.dto";
import { SearchUserDto } from "./dto/search-user.dto";
import { ChangePasswordDto } from "./dto/change-password.dto";
import {
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from "@nestjs/swagger";
import { Role } from "src/common/enums/role.enum";
import { ResponseResult } from "src/common/tools/response.result";
import { Roles } from "src/common/decorators/roles.decorator";
import { UserPageResult } from "./result/user.page.result";
import { UserListResult } from "./result/user.list.result";
import { UserResult } from "./result/user.result";

@ApiTags("用户管理")
@Controller("/system/users")
export class UserController {
  constructor(private readonly userService: UserService) {}

  @ApiOperation({ summary: "分页列表" })
  @ApiQuery({
    name: "keyword",
    description: "关键字",
    required: false,
    type: String,
  })
  @ApiQuery({
    name: "current",
    description: "当前页码",
    required: true,
    type: Number,
  })
  @ApiQuery({
    name: "size",
    description: "每页条数",
    required: true,
    type: Number,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: UserPageResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  getPageList(
    @Headers("tenant-id") tenantId: number,
    @Query("keyword") keyword,
    @Query("current") current,
    @Query("size") size
  ) {
    const searchUserDto = new SearchUserDto();
    searchUserDto.keyword = keyword;
    searchUserDto.page = {
      current,
      size,
    };
    searchUserDto.keyword = keyword;
    return this.userService.getUserPageList(tenantId, searchUserDto);
  }

  @ApiOperation({ summary: "列表" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: UserListResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get("/getUserList")
  async getUserList(@Headers("tenant-id") tenantId: number) {
    const list = await this.userService.getUserList(tenantId);
    return list;
  }

  @ApiOperation({ summary: "详情" })
  @ApiParam({ name: "id", type: String, description: "编号", required: true })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: UserResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(":id")
  getUserById(@Headers("tenant-id") tenantId: number, @Param("id") id: string) {
    return this.userService.getUserById(tenantId, +id);
  }

  @ApiOperation({ summary: "根据用户名称获取详情" })
  @ApiParam({
    name: "userName",
    type: String,
    description: "用户名称",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: UserResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get("getUserByUserName/:userName")
  getUserByUserName(
    @Headers("tenant-id") tenantId: number,
    @Param("userName") userName: string
  ) {
    return this.userService.getUserByUserName(tenantId, userName);
  }

  @ApiOperation({ summary: "创建" })
  @ApiBody({ type: UserDto, description: "创建用户参数" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Post()
  createUser(@Headers("tenant-id") tenantId: number, @Body() userDto: UserDto) {
    return this.userService.createUser(tenantId, userDto);
  }

  @ApiOperation({ summary: "修改" })
  @ApiParam({ name: "id", type: String, description: "编号", required: true })
  @ApiBody({ type: UserDto, description: "修改用户参数" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Put(":id")
  updateUserById(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string,
    @Body() userDto: UserDto
  ) {
    return this.userService.updateUserById(tenantId, +id, userDto);
  }

  @ApiOperation({ summary: "删除" })
  @ApiParam({ name: "id", type: String, description: "编号", required: true })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Delete(":id")
  removeUserById(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string
  ) {
    return this.userService.removeUserById(tenantId, +id);
  }

  @ApiOperation({ summary: "批量删除" })
  @ApiParam({
    name: "ids",
    type: String,
    description: "编号,多个以逗号隔开",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Delete("batchRemove/:ids")
  batchRemove(
    @Headers("tenant-id") tenantId: number,
    @Param("ids") ids: string
  ) {
    return this.userService.removeUserByIds(tenantId, ids);
  }

  @ApiOperation({ summary: "导入" })
  @ApiParam({
    name: "ids",
    type: String,
    description: "编号,多个以逗号隔开",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Post("/importUser")
  @UseInterceptors(FileInterceptor("file"))
  async importUser(
    @Headers("tenant-id") tenantId: number,
    @UploadedFile() file: Express.Multer.File
  ) {
    await this.userService.importExcel(tenantId, file);
  }

  @ApiOperation({ summary: "导出" })
  @ApiParam({
    name: "ids",
    type: String,
    description: "编号,多个以逗号隔开",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Get("/exportUser")
  async exportUser(
    @Headers("tenant-id") tenantId: number,
    @Res() res: Response
  ) {
    const stream = await this.userService.exportExcel(tenantId);
    res.send(stream);
  }

  @ApiOperation({ summary: "启用" })
  @ApiParam({
    name: "id",
    type: String,
    description: "编号",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Patch("enabled/:id")
  enabled(@Headers("tenant-id") tenantId: number, @Param("id") id: string) {
    return this.userService.enabledUserById(tenantId, +id);
  }

  @ApiOperation({ summary: "禁用" })
  @ApiParam({
    name: "id",
    type: String,
    description: "编号",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Patch("disable/:id")
  disable(@Headers("tenant-id") tenantId: number, @Param("id") id: string) {
    return this.userService.disableUserById(tenantId, +id);
  }

  @ApiOperation({ summary: "重置密码" })
  @ApiParam({
    name: "id",
    type: String,
    description: "编号",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Patch("resetPassword/:id")
  resetPassword(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string
  ) {
    return this.userService.resetUserPasswordById(tenantId, +id);
  }

  @ApiOperation({ summary: "修改密码" })
  @ApiParam({
    name: "id",
    type: String,
    description: "编号",
    required: true,
  })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Patch("changePassword/:id")
  changePassword(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string,
    @Body() changePasswordDto: ChangePasswordDto
  ) {
    return this.userService.changePasswordById(
      tenantId,
      +id,
      changePasswordDto
    );
  }
}
